<template>
<div>
  <!--    面包屑导航区域-->
  <el-breadcrumb separator-class="el-icon-arrow-right">
    <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
    <el-breadcrumb-item>班级管理</el-breadcrumb-item>
    <el-breadcrumb-item>查询班级</el-breadcrumb-item>
  </el-breadcrumb>
  <el-card>
    <!--      搜索与添加区域-->
    <el-row :gutter="20">
      <el-col :span="10">
        <el-input placeholder="请输入内容" v-model="queryInfo.searchInfo" clearable @clear="getClassList">
          <el-button slot="append" icon="el-icon-search"></el-button>
        </el-input>
      </el-col>
    </el-row>
    <!--        班级列表区域-->
    <el-table :data="pageclasslist" :stripe="true">
      <el-table-column fixed type="index" label="#"></el-table-column>
        <el-table-column prop="id" label="编号" width="80px"></el-table-column>
      <el-table-column prop="name" label="班级名"></el-table-column>
      <el-table-column prop="teacherName" label="教师姓名">
        <template slot-scope="scope">
          <el-popover>
            <p>教师姓名: {{scope.row.teachName}}</p>
            <hr>
            <p>教师工号： {{scope.row.teachId}}</p>
            <div slot="reference">
              {{scope.row.teachName}}
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column prop="count" label="班级人数"></el-table-column>
      <!--        操作-->
      <el-table-column label="操作" width="120" fixed="right">
        <template slot-scope="scope">
          <!--              修改班级名称按钮-->
          <el-tooltip effect="dark" content="修改班级名称" placement="top">
            <el-button disabled type="primary" icon="el-icon-edit" size="mini" :enterable="false" @click="showNameDialog(scope.row.id)"></el-button>
          </el-tooltip>
          <!--              删除班级按钮-->
          <el-tooltip effect="dark" content="删除班级" placement="top">
            <el-button type="danger" icon="el-icon-delete" size="mini" :enterable="false" @click="removeClassById(scope.row.id)"></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <!--      分页区域-->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[5, 10, 20, 30]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </el-card>
    <!--    修改班级名称的对话框-->
    <el-dialog
      title="修改班级名称"
      :visible.sync="nameDialogVisible"
      width="45%" @close="nameDialogClosed">
      <!--      内容主体区域-->
      <el-form :model="nameForm" status-icon :rules="nameFormRules" ref="nameFormRef" label-width="100px">
        <el-form-item label="班级编号">
          <el-input v-model="nameForm.id" disabled></el-input>
        </el-form-item>
        <el-form-item label="班级新名称" prop="name">
          <el-input v-model="nameForm.name" placeholder="请输入班级名称" @keyup.enter.native="editName" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <!--      底部区域-->
      <span slot="footer" class="dialog-footer">
          <el-button @click="resetName">重置修改</el-button>
          <el-button type="primary" @click="editName">确定修改</el-button>
        </span>
    </el-dialog>
</div>
</template>
<script>
export default {
  name: 'SearchGroup',
  data () {
    var name = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入班级新名称, 不能为空'))
      } else {
        callback()
      }
    }
    return {
      // 获取用户列表的参数对象
      queryInfo: {
        // 模糊搜索
        searchInfo: '',
        // 当前页数
        pagenum: 1,
        pagesize: 5
      },
      classlist: [],
      total: 0,
      nameDialogVisible: false,
      // 班级名称
      nameForm: {
        id: '',
        name: ''
      },
      // 修改班级名称对话框
      nameFormRules: {
        name: [
          {
            validator: name,
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created () {
    this.getClassList()
  },
  methods: {
    async getClassList () {
      const { data: res } = await this.$http.get('/class/all/')
      if (res.status !== 200) {
        return this.$message.error('获取班级列表失败!')
      }
      this.classlist = res.data
      this.total = res.data.length
    },
    // 监听 pagesize 改变的事件
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getClassList()
    },
    // 监听页码值改变的事件
    handleCurrentChange (newPage) {
      // console.log(newPage)
      this.queryInfo.pagenum = newPage
      this.getClassList()
    },
    resetName () {
      this.$refs.nameFormRef.resetFields()
    },
    showNameDialog (id) {
      this.nameDialogVisible = true
      this.nameForm.id = id
    },
    // 监听修改班级名称对话框的关闭事件
    nameDialogClosed () {
      this.$refs.nameFormRef.resetFields()
    },
    // 点击按钮，修改名称
    editName () {
      this.$refs.nameFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.put('/class/updateName/', {
          classId: this.nameForm.id,
          className: this.nameForm.name
        })
        console.log(this.nameForm.name)
        console.log(this.nameForm.id)
        if (res.status !== 200) {
          return this.$message.error('更新班级名称失败!')
        }
        this.$message.success('更新班级名称成功！')
        this.nameDialogVisible = false
        this.getClassList()
      })
    },
    async removeClassById (id) {
      const confirmResult = await this.$confirm('此操作将永久删除该班级, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }
      ).catch(err => err)
      // 若用户确认删除，则返回值为字符串 confirm
      // 若取消删除，返回 cancel
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      const { data: res } = await this.$http.delete('/class/delete/' + id)
      if (res.status !== 200) {
        return this.$message.error('删除班级失败！')
      }
      this.$message.success('删除班级成功!')
      this.getClassList()
    }
  },
  computed: {
    pageclasslist: function () {
      var search = this.queryInfo.searchInfo
      if (search) {
        return this.classlist.filter(function (data) {
          return Object.keys(data).some(function (key) {
            return String(data[key]).toLowerCase().indexOf(search) > -1
          })
        })
      }
      return this.classlist.slice((this.queryInfo.pagenum - 1) * this.queryInfo.pagesize, this.queryInfo.pagenum * this.queryInfo.pagesize)
    }
  }
}
</script>

<style scoped>

</style>
